<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 *
 */
class Auth extends CI_Model  {
	/**
	 * Constructor
	 * @access protected
	 */
	function _construct()
	{
      // Call the Model constructor
      parent::_construct();
	}
	function userlist()
	{
		if($_SESSION['userType']==2)
		{
		$query="Select id,site as name from users where admin<>0";
		$result=$this->db->query($query);
		$responce="
		<script>
		function replace_string(txt,cut_str,paste_str)
		{ 
			var f=0;
			var ht='';
			ht = ht + txt;
			f=ht.indexOf(cut_str);
			while (f!=-1)
			{ 
				//цикл для вырезания всех имеющихся подстрок 
				f=ht.indexOf(cut_str);
				if (f>0)
				{
					ht = ht.substr(0,f) + paste_str + ht.substr(f+cut_str.length);
				};
			};
			return ht
		};
		function changId(id)
		{
			$.ajax({
				url: '/admin/changeUserId/'+id,
					type:'post',
					success:function(data)
					{
						console.log(window.location.href);
						href=replace_string(window.location.href,'/authfunc','');
						console.log(href);
						window.location = href;
						//$(window.location.reload());
					}
			});
		}
		</script>
		";
		$responce.="<select id=users onChange='changId(document.getElementById(\"users\").value)'>";
		foreach($result->result() as $row)
		{
			$responce.="<option value=$row->id ";
			if($row->id==$_SESSION['idUser'])
			{
				$responce.=" selected ";
			}
			$responce.=" >$row->name</option>";
			
		}
		
		$responce.="</select>";
		return $responce;
		}else return "";
	}
	function check($login="",$pass="")
	{
		$responce['state']=false;
		$responce['message']="";
		
		if($_SESSION['is_auth']==true)
		{
			$responce['state']=true;
			return $responce;
		}
		else
		{
			
			$query="Select email from users where name='".$login."'";
			$result=$this->db->query($query);
			if($result->num_rows()>0)
			{
				$email=$result->row()->email;
				$codepass=md5($pass.$email);
				$query="Select id,admin from users where name='".$login."' and password='".$codepass."'";
				$result=$this->db->query($query);
				if($result->num_rows()>0)
				{
					
					$this->db->close();
					$_SESSION['is_auth']=true;
					$responce['state']=true;
					$_SESSION['userType']=$result->row()->admin;
					$_SESSION['idUser']=$result->row()->id;
					return $responce;
				}
				else
				{
					$this->db->close();
					$_SESSION['is_auth']=false;
					$responce['state']=false;
					$_SESSION['userType']=false;
					$_SESSION['idUser']=-1;
					return $responce;
					#$responce['message']="Invalid password";
				}
			}
			else
			{
				$this->db->close();
				$_SESSION['is_auth']=false;
				$responce['state']=false;
				$_SESSION['userType']=false;
				$_SESSION['idUser']=-1;
				return $responce;
				#$responce['message']="Dont have user whith name ".$login;
			}
		}
	}
	function register($login,$password,$email="",$site="",$country="")
	{

		$responce['state']=false;
		$responce['message']="";
		$query="Select COUNT(*) as count_l from users where name='".$login."'";
		$result=$this->db->query($query);
		if($result->num_rows()>0)
		{
			$count=$result->row()->count_l;
			if($count==0)
			{
				$codepass=md5($password.$email);
				$query="INSERT INTO `users` (`name`, `password`, `email`, `site`,`country`) VALUES ( '".$login."', '".$codepass."', '".$email."', '".$site."','".$country."');";
				$this->db->query($query);
				$responce['state']=true;
				$_SESSION['is_auth']=true;
				
			}
			else
			{
				$responce['state']=false;
				$responce['message']="I have user whith name ".$login;
				$_SESSION['is_auth']=false;
			}
		}
		$this->db->close();
		return $responce;
	}
}